- (int)charToInt:(unichar)chr
{
  return (int)chr – 48;
}

- (int)codeCheckSum:(NSString*)code isEAN:(BOOL)isean
{
  int sum = 0;
  int modval = 0;

  for (int x = 0; x < [code length]; ++x)
    {
      int digit = [self charToInt:[code characterAtIndex:x]];
      if (digit >= 10)
	return NO;

      modval = 1;
      if (x % 2 == 1 && isean)
	modval = 3;
      else if (x % 2 == 0 && !isean)
	modval = 4;

      sum += (digit * modval);
    }

  if (isean)
    {
      int modsum = sum % 10;
      int digit = 10 – modsum;
      if (digit == 10)
	digit = 0;

      return digit;
    }

  return sum % 90;
}

- (BOOL)validStickyBit:(NSString*)code
{
  if (!code || code == @"" || [code length] != 13)
    return NO;

  NSString *mainnum = [code substringWithRange:NSMakeRange(1, [code length] – 2)];
  int chk1 = [self charToInt:[code characterAtIndex:0]];
  int chk2 = [self charToInt:[code characterAtIndex:[code length] – 1]];
  if (chk1 > 10 || chk2 > 10)
    return NO;

  int sbchk = [self codeCheckSum:mainnum isEAN:NO];
  int frontnum, backnum;

  if (sbchk < 10)
    {
      frontnum = sbchk;
      backnum = 0;
    }
  else 
    {
      NSString *str = [NSString stringWithFormat:@"%d", sbchk];
      frontnum = [self charToInt:[str characterAtIndex:1]];
      backnum = [self charToInt:[str characterAtIndex:0]];
    }

  if (chk1 != frontnum)
    return NO;

  int eanchk = [self codeCheckSum:[NSString stringWithFormat:"%d%", frontnum, mainnum] isEAN:YES];

  if (eanchk == backnum)
    backnum++;

  if (chk2 != backnum)
    return NO;

  return YES;
}
